package com.firebird.app.kotlin.stock.net

import androidx.annotation.NonNull
import com.firebird.app.kotlin.stock.utils.SharedPreferencesUtil
import com.firebird.app.kotlin.stock.App
import com.firebird.app.kotlin.stock.Config
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.fastjson.FastJsonConverterFactory
import java.io.IOException
import java.util.concurrent.TimeUnit


/**
 * @author ljk
 * @date 2018/4/17.
 * description：
 */
class RetrofitClient private constructor() {
    private var apiService: APIService? = null//添加Token
    /**
     * 设置Header
     *
     * @return
     */
    private val headerInterceptor: Interceptor
        private get() = object : Interceptor {
            @Throws(IOException::class)
            override fun intercept(@NonNull chain: Interceptor.Chain): Response {
                val token = SharedPreferencesUtil.getInstance(App.context!!)!!.getSP("token")
                val debtorId = SharedPreferencesUtil.getInstance(App.context!!)!!.getSP("debtorid")
                val original = chain.request()
                val requestBuilder = original.newBuilder() //添加Token
                        .header("token", token)
                        .header("debtorId", debtorId)
                val request = requestBuilder.build()
                return chain.proceed(request)
            }
        }//显示日志

    /**
     * 设置拦截器
     *
     * @return
     */
    private val interceptor: Interceptor
        private get() {
            val interceptor = HttpLoggingInterceptor()
            //显示日志
            interceptor.level = HttpLoggingInterceptor.Level.BODY
            return interceptor
        }//设置网络请求的Url地址
    //设置数据解析器
    //设置网络请求适配器，使其支持RxJava与RxAndroid
    //创建—— 网络请求接口—— 实例
//设置Header
    //超时时间
    //设置拦截器

    //初始化一个client,不然retrofit会自己默认添加一个
    val api: APIService?
        get() { //初始化一个client,不然retrofit会自己默认添加一个
            val client = OkHttpClient().newBuilder() //设置Header
                    .addInterceptor(headerInterceptor) //超时时间
                    .connectTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS) //设置拦截器
                    .addInterceptor(interceptor)
                    .build()
            val retrofit: Retrofit = Retrofit.Builder()
                    .client(client) //设置网络请求的Url地址
                    .baseUrl(Config.BASEURL) //设置数据解析器
                    .addConverterFactory(FastJsonConverterFactory.create()) //设置网络请求适配器，使其支持RxJava与RxAndroid
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build()
            //创建—— 网络请求接口—— 实例
            apiService = retrofit.create(APIService::class.java)
            return apiService
        }

    companion object {
        @Volatile
        var instance: RetrofitClient? = null
            get() {
                if (field == null) {
                    synchronized(RetrofitClient::class.java) {
                        if (field == null) {
                            field = RetrofitClient()
                        }
                    }
                }
                return field
            }
            private set

    }
}